﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json.Linq;

namespace ZBZH_Manage_Order.PublicMethod
{
    #region 把字串转类
    public static class ParseClass<T> where T : new()
    {
        /// <summary>
        /// 将字符串转换成实体类
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <param name="RETID">字符串类型（1：JSON；2：XML）</param>
        /// <returns></returns>
        public static T StrDeserialize(string Str, int RETID)
        {
            switch (RETID)
            {
                case 1:
                    return JsonDeserialize(Str);
                default:
                    return XMLDeserialize(Str);
            }
        }
        /// <summary>
        ///把字条串转成类
        /// </summary>
        /// <param name="XML"></param>
        /// <returns></returns>
        public static T XMLDeserialize(string XML)
        {
            try
            {
                T 类 = default(T);
                XmlSerializer xer = new XmlSerializer(typeof(T));
                XmlTextReader reader = new XmlTextReader(new StringReader(XML));
                类 = (T)xer.Deserialize(reader);
                return 类;
            }
            catch (Exception ex)
            {
                // WriteLog.WriteErrLog("解析用户的更新操作的数据出现异常：" + ex.Message);
                return default(T);
            }
        }

        public static T JsonDeserialize(string json)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            T obj = default(T);
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
            }
            return obj;
        }

        public static List<T> JsonList(IList<JToken> jieguo)
        {
            List<T> ls = new List<T>();

            Type tp = typeof(T);
            PropertyInfo[] pr = tp.GetProperties();
            for (int i = 0; i < jieguo.Count; i++)
            {
                T cl = new T();
                foreach (PropertyInfo item in pr)
                {
                    object bb = null;
                    try
                    {
                        switch (item.PropertyType.Name.ToString())
                        {
                            case "Int32":
                                bb = (int)jieguo[i][item.Name];
                                break;
                            case "String":
                                bb = (string)jieguo[i][item.Name];
                                
                                break;
                            case "Double":
                                bb = (Double)jieguo[i][item.Name];
                                break;
                            default:
                                if (item.PropertyType.FullName.ToString().Contains("Int"))
                                {
                                    bb = (int)jieguo[i][item.Name];
                                }
                                else
                                {
                                    bb = (string)jieguo[i][item.Name];
                                }
                                break;
                        }
                    }
                    catch (Exception)
                    {
                    }

                    item.SetValue(cl, bb, null);
                }
                ls.Add(cl);
            }
            return ls;
        }
    }
    #endregion
}